package com.xk857.simpleeffect.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.domain.vo.MatterListVO;
import com.xk857.simpleeffect.utils.CalendarUtils;
import com.xk857.simpleeffect.utils.MyDateUtils;

import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

public class ListMatterAdapter extends RecyclerView.Adapter<ListMatterAdapter.ViewHolder> {

    private final Context mContext;
    private final List<MatterListVO> voList;
    private Consumer<Boolean> consumer;

    public ListMatterAdapter(Context context, List<MatterListVO> voList) {
        this.mContext = context;
        this.voList = voList;
    }

    public void setConsumer(Consumer<Boolean> consumer) {
        this.consumer = consumer;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.list_matter, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Date date = new Date(voList.get(position).getMatterDate());
        holder.dayTextView.setText(MyDateUtils.formatDate(date));
        holder.weekTextView.setText(CalendarUtils.getWeek(date, false));

        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        holder.recyclerView.setLayoutManager(layoutManager);
        ListMatterItemAdapter itemAdapter = new ListMatterItemAdapter(mContext, voList.get(position).getMatterList(), consumer);
        holder.recyclerView.setAdapter(itemAdapter);
    }

    @Override
    public int getItemCount() {
        return voList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView dayTextView;
        TextView weekTextView;
        RecyclerView recyclerView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.dayTextView = itemView.findViewById(R.id.day_text);
            this.weekTextView = itemView.findViewById(R.id.week_text);
            this.recyclerView = itemView.findViewById(R.id.recycler_view);
        }
    }
}
